﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace SkiPathFinder
{
    public class PathAnalyzer
    {
        public double StepValue = 100D;

        public double HighUphillElevationGain = 200D;

        public double HighDownhillElevationGain = 200D;

        public double SharpUphillAngle = 45D;

        public double SharpDownhillAngle = 45D;

        public double HighUphillElevationGainWeight = 0.2;

        public double HighDownhillElevationGainWeight = 0.2;

        public double SharpUphillAngleWeight = 0.2;

        public double SharpDownhillAngleWeight = 0.2;

        public double LowUphillElevationGainWeight = 0.05;

        public double LowDownhillElevationGainWeight = 0.05;

        public double SmoothUphillAngleWeight = 0.05;

        public double SmoothDownhillAngleWeight = 0.05;

        List<double> uphillArray = new List<double>();
        List<double> downhillArray = new List<double>();
        List<double> sharpUphillArray = new List<double>();
        List<double> sharpDownhillArray = new List<double>();


        DataTable ElevationTable = new DataTable();

        /// This function is used to calculate the total distance of Highuphill elevation,Highdownhill elevation and sharp uphill and sharp downhill
      
        public DataTable AnalyzePathRisk(double highUphillElevationGain,double highdownElevationGain)
        {
            double HighUphillElevationGain = 0.0;
            double NormalUphillElevationGain = 0.0;
            double HighDownlElevationGain = 0.0;
            double NormallDownElevationGain = 0.0;

          
            for (int j = 0; j < uphillArray.Count; j++)
            {
                if (uphillArray[j] >= highUphillElevationGain)
                {
                    HighUphillElevationGain += uphillArray[j];
                }
                else
                {
                    NormalUphillElevationGain += uphillArray[j];
                }
            }


            for (int k = 0; k < downhillArray.Count; k++)
            {
                double downhillValue= Math.Abs(Convert.ToDouble(downhillArray[k]));

                if (downhillValue>= highdownElevationGain)
                {
                    HighDownlElevationGain += downhillArray[k];
                }
                else
                {
                    NormallDownElevationGain += downhillArray[k];
                }
            }


            DataTable RiskAnalyzeTable = new DataTable();
            RiskAnalyzeTable.Columns.Add(new DataColumn("HighUphillElevationGain"));
            RiskAnalyzeTable.Columns.Add(new DataColumn("NormalUphillElevationGain"));
            RiskAnalyzeTable.Columns.Add(new DataColumn("HighDownhillElevationGain"));
            RiskAnalyzeTable.Columns.Add(new DataColumn("NormalDownhillElevationGain"));

            DataRow row = RiskAnalyzeTable.NewRow();
            row[0] = Math.Round(HighUphillElevationGain, 0);
            row[1] = Math.Round(NormalUphillElevationGain, 0);
            row[2] = Math.Round(HighDownlElevationGain, 0);
            row[3] = Math.Round(NormallDownElevationGain, 0);

            RiskAnalyzeTable.Rows.Add(row);

            return RiskAnalyzeTable;


        }


        //public DataTable AnalyzePath(DataTable table)
        //{
        //    List<double> uphillArray = new List<double>();
        //    List<double> downhillArray = new List<double>();
        //    List<double> sharpUphillArray = new List<double>();
        //    List<double> sharpDownhillArray = new List<double>();

        //    //The path distance, uphill elevation gain and downhill elevation gain
        //    double uphillElevationGain = 0.0;
        //    double downhillElevationGain = 0.0;
        //    double curElevDiff = 0.0;

        //    for(int r = 1; r < table.Rows.Count; r++)
        //    {
        //        curElevDiff = Convert.ToDouble(table.Rows[r][1]) - Convert.ToDouble(table.Rows[r-1][1]);

        //        if (curElevDiff >= 0)
        //        {
        //            uphillElevationGain += curElevDiff;
        //            uphillArray[r] = curElevDiff;
        //        }
        //        else
        //        {
        //            downhillElevationGain += curElevDiff;
        //            downhillArray = curElevDiff;
        //        }
        //    }

        //    double pathDistance = Convert.ToDouble(table.Rows[table.Rows.Count - 1][0]);

        //    DataTable outputTable = new DataTable();
        //    outputTable.Columns.Add(new DataColumn("distance"));
        //    outputTable.Columns.Add(new DataColumn("uphillElevation"));
        //    outputTable.Columns.Add(new DataColumn("downhillElevation"));

        //    DataRow row = outputTable.NewRow();
        //    row[0] = Math.Round(pathDistance,0);
        //    row[1] = Math.Round(uphillElevationGain,0);
        //    row[2] = Math.Round(downhillElevationGain,0);

        //    outputTable.Rows.Add(row);

        //    return outputTable;
        //}

        public DataTable AnalyzePath(DataTable table)
        {
           
            //The path distance, uphill elevation gain and downhill elevation gain
            double uphillElevationGain = 0.0;
            double downhillElevationGain = 0.0;
            double curElevDiff = 0.0;
            double currentsolpe;
            double stepDistance;
            
            //Get the total distance of the path
             double pathDistance = Convert.ToDouble(table.Rows[table.Rows.Count - 1][0]);
             
            stepDistance = pathDistance / Convert.ToDouble(table.Rows.Count) ;

            for (int r = 1; r < table.Rows.Count; r++)
            {
                //Get the elevation
                curElevDiff = Convert.ToDouble(table.Rows[r][1]) - Convert.ToDouble(table.Rows[r - 1][1]);

               if (curElevDiff >= 0)
                {
                    uphillElevationGain += curElevDiff;
                    uphillArray.Add(curElevDiff);
                }
                else
                {
                    downhillElevationGain += curElevDiff;
                    downhillArray.Add(curElevDiff);
                }

           }

          
            DataTable outputTable = new DataTable();
            outputTable.Columns.Add(new DataColumn("distance"));
            outputTable.Columns.Add(new DataColumn("uphillElevation"));
            outputTable.Columns.Add(new DataColumn("downhillElevation"));

            DataRow row = outputTable.NewRow();
            row[0] = Math.Round(pathDistance, 0);
            row[1] = Math.Round(uphillElevationGain, 0);
            row[2] = Math.Round(downhillElevationGain, 0);

            outputTable.Rows.Add(row);

            return outputTable;
        }
    }
}
